
import {
    toast
} from 'nc-lightapp-front';

let currentOptSelectedRows = null;

export default class OfferAction {
    constructor(comp) {
        this.comp = comp;
    }

    // 打开offer弹窗
    openOfferModal = async (selectedRows) => {

        const {props} = this.comp;
        const {dispatch, emp} = props;

        let content = await this.getOfferContent(selectedRows);
        currentOptSelectedRows = selectedRows;
        if(content) {
            dispatch({
                type: 'emp/update',
                payload: {
                    offerContentModalVisible: true,
                    offerModalContent: content
                }
            });
        }
    }

    // 关闭offer弹窗
    closeOfferModal = () => {
        const {props} = this.comp;
        const {dispatch, emp} = props;

        currentOptSelectedRows = null;

        dispatch({
            type: 'emp/update',
            payload: {
                offerContentModalVisible: false,
                offerModalContent: ''
            }
        });
    }

    // 发送offer
    sendOffer = async (selectedRows) => {
        const {props} = this.comp;
        const {dispatch, emp} = props;

        if(!selectedRows && currentOptSelectedRows) {
            selectedRows = currentOptSelectedRows;
        }

        let postData = {
            billid: selectedRows[0].values['pk_entryapply'].value,
            pk_org: emp.orgValue.refpk
        };

        try {
            let res = await dispatch({
                type: 'emp/sendOffer',
                payload: {
                    postData: postData
                }
            });

            if(res.success) {
                toast({
                    color: 'success',
                    content: emp.language['gx6008-000052'] // 操作成功
                });
            }
        }
        catch(e) {

        }
    }

    // 获取offer内容
    getOfferContent = async (selectedRows) => {
        const {props} = this.comp;
        const {dispatch, emp} = props;

        let postData = {
            billid: selectedRows[0].values['pk_entryapply'].value,
            pk_org: emp.orgValue.refpk
        };

        try {
            let res = await dispatch({
                type: 'emp/getOfferModalContent',
                payload: {
                    postData: postData
                }
            });

            if(res.success) {
                return res.data;
            }
        }
        catch(e) {
        }
    }
    
    // 接受offer
    acceptOffer = async (selectedRows) => {
        const {props} = this.comp;
        const {dispatch, emp} = props;

        let postData = {
            billid: selectedRows[0].values['pk_entryapply'].value,
            pk_org: emp.orgValue.refpk
        };

        try {
            let res = await dispatch({
                type: 'emp/acceptOffer',
                payload: {
                    postData: postData
                }
            });

            if(res.success) {
                toast({
                    color: 'success',
                    content: emp.language['gx6008-000052'] // 操作成功
                });
            }
        }
        catch(e) {
        }
    }

    // 拒绝offer
    rejectOffer = async (selectedRows) => {
        const {props} = this.comp;
        const {dispatch, emp} = props;

        let postData = {
            billid: selectedRows[0].values['pk_entryapply'].value,
            pk_org: emp.orgValue.refpk
        };

        try {
            let res = await dispatch({
                type: 'emp/rejectOffer',
                payload: {
                    postData: postData
                }
            });

            if(res.success) {
                toast({
                    color: 'success',
                    content: emp.language['gx6008-000052'] // 操作成功
                });
            }
        }
        catch(e) {
        }
    }

    // 提交offer
    submitOffer = async (selectedRows) => {
        const {props} = this.comp;
        const {dispatch, emp} = props;

        let postData = {
            billid: selectedRows[0].values['pk_entryapply'].value,
            pk_org: emp.orgValue.refpk
        };

        try {
            let res = await dispatch({
                type: 'emp/submitOffer',
                payload: {
                    postData: postData
                }
            });

            if(res.success) {
                toast({
                    color: 'success',
                    content: emp.language['gx6008-000052'] // 操作成功
                });
            }
        }
        catch(e) {
        }
    }
}